package com.shaoshaohuo.app.view.citymenu;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.db.DaoUtils;
import com.shaoshaohuo.app.framework.log.LogUtil;
import com.shaoshaohuo.app.view.citymenu.adpater.MenuItemAdapter;
import com.shaoshaohuo.app.view.citymenu.entity.Area;
import com.shaoshaohuo.app.view.citymenu.interfaces.CascadingMenuViewOnSelectListener;

import java.util.ArrayList;
import java.util.List;

/**
 * 三级级联动ListView
 */
public class CascadingMenuView extends LinearLayout {
    private static final String TAG = CascadingMenuView.class.getSimpleName();
    // 三级菜单选择后触发的接口，即最终选择的内容
    private CascadingMenuViewOnSelectListener mOnSelectListener;
    private ListView firstMenuListView;
    private ListView secondMenuListView;
    private ListView thirdMenuListView;
    private boolean isWithCountry = true;

    private CityMenuDecorator decorator;

    // 每次选择的子菜单内容
    private List<Area> thirdItemList = new ArrayList<Area>();
    private List<Area> secondItemList = new ArrayList<Area>();
    private List<Area> firstItemList = new ArrayList<Area>();

    private MenuItemAdapter firstMenuListViewAdapter;

    private MenuItemAdapter secondMenuListViewAdapter;

    private MenuItemAdapter thirdMenuListViewAdapter;

    private int firstPosition = 0;
    private int secondPosition = 0;
    private int thirdPosition = 0;

    private Context context;

    /**
     * @param context 上下文
     */
    public CascadingMenuView(Context context) {
        this(context, false);
    }

    public CascadingMenuView(Context context, boolean isWithCountry) {
        super(context);
        this.context = context;
        this.isWithCountry = isWithCountry;
        this.decorator = new CityMenuDecorator();
        init(context);
    }

    public CascadingMenuView(Context context, AttributeSet attrs) {
        this(context, attrs, false);
    }

    public CascadingMenuView(Context context, AttributeSet attrs, boolean isWithCountry) {
        super(context, attrs);
        this.context = context;
        this.isWithCountry = isWithCountry;
        this.decorator = new CityMenuDecorator();
        init(context);
    }

    private void init(final Context context) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.view_region, this, true);
        firstMenuListView = (ListView) findViewById(R.id.listView);
        secondMenuListView = (ListView) findViewById(R.id.listView2);
        thirdMenuListView = (ListView) findViewById(R.id.listView3);
        // setBackgroundDrawable(getResources().getDrawable(
        // R.drawable.choosearea_bg_left));

        firstItemList = DaoUtils.findSubAreas(null);

        if (isWithCountry) {
            firstItemList = decorator.decorateAreaList(firstItemList);
        }

        // 初始化一级主菜单
        firstMenuListViewAdapter = new MenuItemAdapter(context, firstItemList,
                R.drawable.choose_item_selected,
                R.drawable.choose_eara_item_selector);
        firstMenuListViewAdapter.setTextSize(17);
        firstMenuListViewAdapter.setSelectedPositionNoNotify(firstPosition,
                firstItemList);
        firstMenuListView.setAdapter(firstMenuListViewAdapter);
        firstMenuListViewAdapter
                .setOnItemClickListener(new MenuItemAdapter.OnItemClickListener() {

                    @Override
                    public void onItemClick(View view, int position) {
                        // 选择主菜单，清空原本子菜单内容，增加新内容
                        int oldFirstPosition = firstPosition;
                        firstPosition = position;
                        secondPosition = 0;
                        thirdPosition = 0;
                        if (isWithCountry) {
                            if (firstPosition == 0) {
                                secondItemList = decorator.getWholeCountList();
                            } else {
                                if (isZhiXiaShi(firstItemList.get(firstPosition).getName())) {
                                    secondItemList = DaoUtils.findSubAreas(firstItemList.get(firstPosition).getAreaId());
                                } else {
                                    secondItemList = DaoUtils.findSubAreas(firstItemList.get(firstPosition).getAreaId());
                                    secondItemList = decorator.decorateAreaList(secondItemList, firstItemList.get(firstPosition));
                                }
                            }
                        } else {
                            Area firstArea = firstItemList.get(firstPosition);
                            secondItemList = DaoUtils.findSubAreas(firstArea.getAreaId());
                        }
                        if (secondItemList != null) {
                            LogUtil.d("CascadingMenuView", "" + secondItemList.size());
                        }
                        // 通知适配器刷新
                        secondMenuListViewAdapter.setSelectedPositionNoNotify(
                                secondPosition, secondItemList);

                        if (isWithCountry) {
                            if (firstPosition == 0) {
                                thirdItemList = decorator.getWholeCountList();
                            } else {
                                if (isZhiXiaShi(firstItemList.get(firstPosition).getName())) {
                                    thirdItemList = DaoUtils.findSubAreas(secondItemList.get(secondPosition).getAreaId());
                                    thirdItemList = decorator.decorateAreaList(thirdItemList, secondItemList.get(secondPosition));
                                } else {
                                    thirdItemList = decorator.decorateAreaList(firstItemList.get(firstPosition));
                                }
                            }
                        } else {
                            thirdItemList = DaoUtils.findSubAreas(secondItemList.get(secondPosition).getAreaId());
                            thirdItemList = decorator.decorateAreaList(thirdItemList, secondItemList.get(secondPosition));
                        }
                        // 通知适配器刷新
                        thirdMenuListViewAdapter.setSelectedPositionNoNotify(thirdPosition,
                                thirdItemList);
                    }
                });
        // 初始化二级主菜单

        if (isWithCountry) {
            secondItemList = decorator.getWholeCountList();
        } else {
            secondItemList = DaoUtils.findSubAreas(firstItemList.get(firstPosition)
                    .getAreaId());
        }
        // Log.i("wer", firstItemList.get(firstPosition));
        if (!secondItemList.isEmpty()) {
            LogUtil.d("CascadingMenuView", secondItemList.get(secondPosition).toString());

            // 初始化二级主菜单
            secondMenuListViewAdapter = new MenuItemAdapter(context, secondItemList,
                    R.drawable.choose_item_selected,
                    R.drawable.choose_eara_item_selector);
            secondMenuListViewAdapter.setTextSize(15);
            secondMenuListViewAdapter.setSelectedPositionNoNotify(secondPosition,
                    secondItemList);
            secondMenuListView.setAdapter(secondMenuListViewAdapter);
            secondMenuListViewAdapter
                    .setOnItemClickListener(new MenuItemAdapter.OnItemClickListener() {

                        @Override
                        public void onItemClick(View view, final int position) {
                            // 选择主菜单，清空原本子菜单内容，增加新内容
                            int oldSecondPosition = secondPosition;
                            secondPosition = position;
                            thirdPosition = 0;
                            if (isWithCountry) {
                                if (firstPosition == 0 && secondPosition == 0) {
                                    thirdItemList = decorator.getWholeCountList();
                                } else if (secondPosition == 0 && isZhiXiaShi(firstItemList.get(firstPosition).getName())) {
                                    thirdItemList = DaoUtils.findSubAreas(secondItemList.get(position).getAreaId());
                                    thirdItemList = decorator.decorateAreaList(thirdItemList, secondItemList.get(secondPosition));
                                } else if (secondPosition == 0) {
                                    thirdItemList = decorator.decorateAreaList(secondItemList.get(secondPosition));
                                } else {
                                    thirdItemList = DaoUtils.findSubAreas(secondItemList.get(position).getAreaId());
                                    thirdItemList = decorator.decorateAreaList(thirdItemList, secondItemList.get(secondPosition));
                                }
                            } else {
                                thirdItemList = DaoUtils.findSubAreas(secondItemList.get(position).getAreaId());
                                thirdItemList = decorator.decorateAreaList(thirdItemList, secondItemList.get(secondPosition));
                            }
                            // 通知适配器刷新
                            thirdMenuListViewAdapter.setSelectedPositionNoNotify(thirdPosition,
                                    thirdItemList);
                        }
                    });
            // 初始化三级主菜单
            if (isWithCountry) {
                thirdItemList = decorator.getWholeCountList();
            } else {
                thirdItemList = DaoUtils.findSubAreas(secondItemList.get(secondPosition).getAreaId());
                thirdItemList = decorator.decorateAreaList(thirdItemList, secondItemList.get(secondPosition));
            }
            if (!thirdItemList.isEmpty()) {
                LogUtil.d("CascadingMenuView", thirdItemList.get(thirdPosition).toString());
                // 初始化三级主菜单
                thirdMenuListViewAdapter = new MenuItemAdapter(context, thirdItemList,
                        R.drawable.choose_item_right,
                        R.drawable.choose_plate_item_selector);
                thirdMenuListViewAdapter.setTextSize(13);
                thirdMenuListViewAdapter.setSelectedPositionNoNotify(thirdPosition,
                        thirdItemList);
                thirdMenuListView.setAdapter(thirdMenuListViewAdapter);
                thirdMenuListViewAdapter
                        .setOnItemClickListener(new MenuItemAdapter.OnItemClickListener() {

                            @Override
                            public void onItemClick(View view, final int position) {
                                thirdPosition = position;
                                Area thirdArea = thirdItemList.get(position);
                                if (mOnSelectListener != null) {
//                                    //选择全国、市辖区时，返回上级menuItem
//                                    if (secondPosition >= secondItemList.size()) {
//                                        secondPosition = 0;
//                                    }
//                                    if (thirdArea.getCity() == null) {
//                                        thirdArea = secondItemList.get(secondPosition);
//                                    }
                                    mOnSelectListener.getValue(thirdArea, getFullName());
                                }
                            }
                        });
            }
        }

        // 设置默认选择
//        setDefaultSelect();
    }

    public String getFullName() {
        Area firstMenuItem = firstItemList.get(firstPosition);
        Area secondMenuItem = secondItemList.get(secondPosition);
        Area thirdMenuItem = thirdItemList.get(thirdPosition);
        StringBuilder sb = new StringBuilder();
        if (firstMenuItem == null) {
            return sb.toString();
        }
        String quanguo = "全国";
        if (quanguo.equals(firstMenuItem.getName())) {
            return quanguo;
        }
        if ("1".equals(thirdMenuItem.getLevelType())) {
            return thirdMenuItem.getName();
        }
        if (secondMenuItem != null && secondMenuItem.getName().indexOf(firstMenuItem.getName()) == 0) {
            sb.append(secondMenuItem.getName());
        } else {
            sb.append(firstMenuItem.getName());
            sb.append(secondMenuItem.getName());
        }
        if (thirdMenuItem != null && !secondMenuItem.getName().equals(thirdMenuItem.getName())) {
            sb.append(thirdMenuItem.getName());
        }
        return sb.toString();
    }


    public void setDefaultSelect() {
        firstMenuListView.setSelection(firstPosition);
        secondMenuListView.setSelection(secondPosition);
        thirdMenuListView.setSelection(thirdPosition);
    }

    public void setCascadingMenuViewOnSelectListener(
            CascadingMenuViewOnSelectListener onSelectListener) {
        mOnSelectListener = onSelectListener;
    }

    private boolean isZhiXiaShi(String name) {
        if ("北京".equals(name) || "天津".equals(name) || "上海".equals(name) || "重庆".equals(name)) {
            return true;
        }
        return false;
    }
}
